package library.common.util;

import android.view.View;

import com.jakewharton.rxbinding.view.RxView;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import rx.Observer;

/**
 * @author Created by qiang.hou on 2018/10/10.
 * @version 1.0
 */
public class MultiClickUtils {

    /**
     * @param view      view
     * @param listener  View.OnClickListener
     * @param clickDiff TimeUnit.MILLISECONDS
     */
    public static void onClick(final View view, final View.OnClickListener listener, long clickDiff) {
        if (null == view || clickDiff <= 0) {
            return;
        }
        RxView.clicks(view)
                .throttleFirst(clickDiff, TimeUnit.MILLISECONDS)
                .subscribe(new Observer<Void>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(Void aVoid) {
                        if (null != listener) {
                            listener.onClick(view);
                        }
                    }
                });
    }

    /**
     * @param view      view
     * @param listener  View.OnLongClickListener
     * @param clickDiff TimeUnit.MILLISECONDS
     */
    public static void onLongClick(final View view, final View.OnLongClickListener listener, long clickDiff) {
        if (null == view || clickDiff <= 0) {
            return;
        }
        RxView.clicks(view)
                .throttleFirst(clickDiff, TimeUnit.MILLISECONDS)
                .subscribe(new Observer<Void>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(Void aVoid) {
                        if (null != listener) {
                            listener.onLongClick(view);
                        }
                    }
                });
    }


    private static long DIFF = 5000;
    private static Map<Integer, Click> clicks = new LinkedHashMap<>();

    /**
     * 判断两次点击的间隔，如果小于1000，则认为是多次无效点击
     *
     * @return boolean
     */
    public static boolean isFastDoubleClick(int buttonId) {
        return isFastDoubleClick(buttonId, DIFF);
    }

    /**
     * 判断两次点击的间隔，如果小于diff，则认为是多次无效点击
     *
     * @param diff 时间间隔
     * @return boolean
     */
    public static boolean isFastDoubleClick(int buttonId, long diff) {
        if (buttonId <= 0 || diff <= 0) {
            return false;
        }
        /*
         * current click time
         */
        long time = System.currentTimeMillis();
        if (clicks.containsKey(buttonId)) {
            Click click = clicks.get(buttonId);
            long timeD = time - click.lastClickTime;
            if (timeD < diff) {
                return true;
            } else {
                click.lastClickTime = time;
            }
        } else {
            for (Map.Entry entry : clicks.entrySet()) {
                if (time - ((Click) entry.getValue()).lastClickTime >= ((Click) entry.getValue()).diff) {
                    clicks.remove(entry.getKey());
                }
            }
            clicks.put(buttonId, new Click(diff, time));
        }
        return false;
    }

    private static class Click {
        private long diff;
        private long lastClickTime;

        public Click(long diff, long lastClickTime) {
            this.diff = diff;
            this.lastClickTime = lastClickTime;
        }
    }
}
